package com.wjbgn.electronicnian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.convert.NianWhereToGoDoConvert;
import com.wjbgn.electronicnian.dto.NianWhereToGoDTO;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianDO;
import com.wjbgn.electronicnian.entity.NianWhereToGoDO;
import com.wjbgn.electronicnian.enums.TwoChoiceOneEnum;
import com.wjbgn.electronicnian.service.NianService;
import com.wjbgn.electronicnian.service.NianWhereToGoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Description:
 * Create Date: 2022-01-20T11:06:12.895428300
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/nianwheretogo")
@AllArgsConstructor
public class NianWhereToGoController {

    /**
     * NianWhereToGoService
     */
    private NianWhereToGoService nianWhereToGoService;

    private NianService nianService;

    /**
     * 去串门
     *
     * @param nainId
     * @return
     */
    @ApiOperation(value = "去串门")
    @GetMapping("/whereToGo")
    public Result whereToGo(@RequestParam("nianId") long nainId) {
        return nianWhereToGoService.whereToGo(nainId);
    }

    /**
     * 分页列表
     *
     * @param nianWhereToGoDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianWhereToGoDTO nianWhereToGoDTO) {
        QueryWrapper<NianWhereToGoDO> queryWrapper = new QueryWrapper<>(NianWhereToGoDoConvert.dtoToDo(nianWhereToGoDTO));
        Page<NianWhereToGoDO> page = new Page<>(nianWhereToGoDTO.getPage(), nianWhereToGoDTO.getLimit());
        Page<NianWhereToGoDO> pageList = nianWhereToGoService.page(page, queryWrapper);
        return Result.success(NianWhereToGoDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     *
     * @param nianId
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(@RequestParam("nianId") long nianId) {
        List<NianWhereToGoDO> list = nianWhereToGoService.lambdaQuery().eq(NianWhereToGoDO::getNianId, nianId).orderByDesc(NianWhereToGoDO::getCreateTime).last("limit 15").list();
        List<NianWhereToGoDTO> nianWhereToGoDTOS = NianWhereToGoDoConvert.listConvert(list);
        Set<Long> nianIds = new HashSet<>();
        if (nianWhereToGoDTOS.size() == 0){
            return Result.success(nianWhereToGoDTOS);
        }
        nianWhereToGoDTOS.forEach(s -> nianIds.add(s.getNianWhereAre()));
        List<NianDO> nianDOS = nianService.listByIds(nianIds);
        Map<Long, String> collect = nianDOS.stream().collect(Collectors.toMap(NianDO::getId, NianDO::getNianName));
        nianWhereToGoDTOS.forEach(l -> {
            if (l.getNianWhatDid() == TwoChoiceOneEnum.WORDS1.getCode()) {
                l.setDescription("我去了" + collect.get(l.getNianWhereAre()) + "家，并学了一句话【" + l.getNianWords() + "】。");
            } else {
                l.setDescription("我去了" + collect.get(l.getNianWhereAre()) + "家，并偷了一个【" + l.getNianDumplings() + "】。");
            }
        });
        return Result.success(nianWhereToGoDTOS);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id") Long id) {
        NianWhereToGoDO nianWhereToGo = nianWhereToGoService.getById(id);
        return Result.success(null == nianWhereToGo ? null : NianWhereToGoDoConvert.doToDto(nianWhereToGo));
    }

    /**
     * 新增
     *
     * @param nianWhereToGoDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianWhereToGoDTO nianWhereToGoDTO) {
        boolean flag = nianWhereToGoService.save(NianWhereToGoDoConvert.dtoToDo(nianWhereToGoDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 更新
     *
     * @param nianWhereToGoDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianWhereToGoDTO nianWhereToGoDTO) {
        boolean flag = nianWhereToGoService.updateById(NianWhereToGoDoConvert.dtoToDo(nianWhereToGoDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }
}